if (!jsx3.lang.Class.forName("ux.cdb.admin.queries.loan.Query")) {
    
jsx3.Class.defineClass(
    "ux.cdb.admin.queries.loan.Query", jsx3.gui.Block, [],
    function (query) {

		query.prototype.initialize = function(){
		}
		query.prototype._initialize = function(){
			var me= this;
			this.queryUtil = ux.cdb.admin.queries.QueryUtil;
			this.matrix = this.get("mtxMain");
      //this.matrix.onSelect = this.bind(me.matrixSelected, me);
      
      this.blkField_studentName = this.get('blkField_studentName').getChild(1);
      this.blkField_status_select = this.get('blkField_status').getChild(1);
      this.blkField_orgName = this.get('blkField_orgName').getChild(1);
      this.blkField_zizhuCenter = this.get('blkField_zizhuCenter').getChild(1);
      this.blkField_applyStart = this.get('blkField_applyStart').getChild(2);
	  this.blkField_applyStart.setDate(new Date());
      this.blkField_applyEnd = this.get('blkField_applyEnd').getChild(1);
	  this.blkField_applyEnd.setDate(new Date());
      this.blkField_certId = this.get('blkField_certId').getChild(1);
      this.blkField_applyId = this.get('blkField_applyId').getChild(1);
      			
      this.loadFilter();	
      this.blkField_status_select.setValue("");
      
      this.pageCtrler = this.get("blkPageWheel");
        
		this.defaultPageSize = 20;
		this.totalRecordCount = 0;
		this.currentPage = 1;
		this.pageControlInit = false;
		this.totalPages = 0; 
            
		ux.cdb.admin.CommonCache.loadCache(['INTERNAL_ORG','GENERAL_APP_STATUS'],me,
			function(){
				var _cache = ux.cdb.admin.CommonCache.getInstance();
				me.get('blkField_orgName').getChild(1).setSourceXML(_cache.getCache('CommonCache_INTERNAL_ORG'));
				me.get('blkField_status').getChild(1).setSourceXML(_cache.getCache('CommonCache_GENERAL_APP_STATUS'));
				me.get('blkField_orgName').getChild(1).repaint();
				me.get('blkField_status').getChild(1).repaint();
				me.get('blkField_zizhuCenter').getChild(1).setSourceXML(jsx3.xml.CDF.Document.newDocument().insertRecord(this.queryUtil.getDefaultOption()));
				me.get('blkField_zizhuCenter').getChild(1).repaint();
			});
	
        //this.pageCtrler.initialize();
        //this.pageCtrler.setValue(this.currentPage,this.totalPages);
        //this.pageCtrler.setValue(this.currentPage,this.totalPages,this.defaultPageSize,this.totalRecordCount);
        //this.pagebar.bindWithMatrix(this, this.loadContractList, this.matrix);          
      		
      //this.refreshMtxData();
      //this.blkField_status_select.OnChange = this.bind(me._onFilter, me);
      //alert(this.blkField_status_select.OnChange);					
		};
		
		query.prototype.displayTip = function(passed) {
			if(!passed) {
				var tip = ux.megatron.Main.getServer().getDynamicProperty("@cdb@queries@TipUnFinished");
				this.get('_messageLable').setText(tip,true);
			} else {
				this.get('_messageLable').setText("",true);
			}
		};
		
		query.prototype.onSearch = function(){
			if(this.get('blkDetail').doValidate() !== null){
				this.displayTip(false);
				return;
			}else{
				this.displayTip(true);
			}
	
			//alert(1);
			var orgName = this.blkField_orgName.getValue();
			var zizhuCenter = this.blkField_zizhuCenter.getValue();
			var applyStart = this.blkField_applyStart.getDate();
			var applyEnd = this.blkField_applyEnd.getDate();
			var studentName = this.blkField_studentName.getValue();
			var certId = this.blkField_certId.getValue();
			var applyId = this.blkField_applyId.getValue();
			var applystatus = this.blkField_status_select.getValue();
			
			orgName = this.emptyToNull(orgName);
			zizhuCenter = this.emptyToNull(zizhuCenter);
			studentName = this.emptyToNull(studentName);
			certId = this.emptyToNull(certId);
			applyId = this.emptyToNull(applyId);
			applystatus = this.emptyToNull(applystatus);
			
			var params = [];
			params.push(orgName);
			params.push(zizhuCenter);
			params.push(applyStart);
			params.push(applyEnd);
			params.push(studentName);
			params.push(certId);
			params.push(applyId);
			params.push(applystatus);
			params.push(this.currentPage);
			params.push(this.defaultPageSize);
			
			var me = this;
			ux.megatron.util.Dwr.callDwr("RetailStudentLoanAppnfoQueryUCC", "getStudentAppListByCondition", 
          params, me, me.onRefreshMtxDataCallBack, "RetailStudentLoanAppnfoQueryUCC");
		}
	
		query.prototype.emptyToNull = function(sValue) {
      	var result = null;
      	if(sValue != null && sValue.trim().length > 0){
      		result = sValue;
      	}
      	return result;
    }
	
		query.prototype.refreshMtxData = function(){
			var me = this;
		      var params = ['', 2222, 333,new Date(), new Date(),11];
		      ux.megatron.util.Dwr.callDwr("RetailStudentLoanAppnfoQueryUCC", "getStudentAppListByCondition", 
          params, me, me.onRefreshMtxDataCallBack, "RetailStudentLoanAppnfoQueryUCC");
		}
	
		query.prototype.reset = function(){
			this.get("blkField_orgName").getChild(1).setValue('');
			this.get("blkField_zizhuCenter").getChild(1).setValue('');	
			this.get("blkField_studentName").getChild(1).setValue(null);
			this.get("blkField_certId").getChild(1).setValue(null);	
			this.get("blkField_applyId").getChild(1).setValue(null);
			this.get("blkField_status").getChild(1).setValue('');	
			this.get("blkField_applyStart").getChild(2).setDate(new Date());
			this.get("blkField_applyEnd").getChild(1).setDate(new Date());	
		}
	
		query.prototype.onRefreshMtxDataCallBack = function(reData){
			if(!reData)
					jsx3.log("call failed......");

		  var flag = reData.res.flag;
		  if(flag != 1){
			var msg = reData.res.msg;
/* @JSC :: begin DEBUG */
			jsx3.log("call failed: "+msg);
/* @JSC :: end */
		  }
			
		  var reList = reData.content.data; 
/* @JSC :: begin DEBUG */
			console.log('onRefreshMtxDataCallBack : \n', reList);
/* @JSC :: end */
			var cdfDoc = ux.megatron.util.Dwr.toCdfDocument(reList,"jsxroot",null);
			var records = cdfDoc.selectNodes("//record");
			for(var i=0;i<records.size(); i++){
					var record = records.get(i);
					record.setAttribute("jsxid",i+1);				
			}
			this.matrix.setSourceXML(cdfDoc);
			this.matrix.repaint();
			this.matrix.doSort(jsx3.gui.Matrix.SORT_ASCENDING);
			
			this.totalRecordCount = reData.content.page.count;
			this.totalPages = (this.totalRecordCount%this.defaultPageSize>0)?(parseInt(this.totalRecordCount/this.defaultPageSize)+1):(this.totalRecordCount/this.defaultPageSize);
			if(this.totalPages == 0)this.totalPages = 1;
			this.loadPageControler(); 
		}
	
		query.prototype.loadPageControler = function() {
			 if(!this.pageControlInit){
					this.pageCtrler.initialize();
					this.pageControlInit = true;
				}
				//this.pageCtrler.setValue(this.currentPage,toltopages,this.defaultPageSize,this.totalRecordCount);
				this.pageCtrler.setValue(this.currentPage,this.totalPages,this.defaultPageSize, this.totalRecordCount);
				var me = this;
				this.pageCtrler.bindWithMatrix(me,me.onPageChange,me.matrix);
		}
	
		query.prototype.onPageChange = function(msg) {
      	this.currentPage = msg.currentPageNumber;
				this.onSearch();
		}
		
		query.prototype.loadCooperOrgList = function(recordId){
			if(!recordId) return;
			var cooperOrgBox = this.get('blkField_zizhuCenter').getChild(1);
			cooperOrgBox.clearXmlData();
			cooperOrgBox.setSourceXML(jsx3.xml.CDF.Document.newDocument().insertRecord({jsxid:'',jsxtext:'Loading...',jsxstyle:'width:100%;'}));
			cooperOrgBox.repaint();
			var option = [{
				dwrClass:'RetailInfoQueryUCC',
				dwrFunction:'getInitCooperOrg',
				dwrParams:[recordId]
			}];
			this.queryUtil.loadCustomizeCache(option, this, function(){
				var cdf = this.queryUtil.getInstance().getCacheAsCDF(0, {jsxid:'partyId', jsxtext:'chnFullName'});
				cooperOrgBox.setSourceXML(cdf);
				cooperOrgBox.repaint();
/* @JSC :: begin DEBUG */
				jsx3.log('loadCustomizeCache finished. cdf : ' + cdf);
/* @JSC :: end */
			});
/* @JSC :: begin DEBUG */
			jsx3.log('loadCooperOrgList recordId : ' + recordId);
/* @JSC :: end */
		};
	
		query.prototype.matrixSelected = function(mtx,strRECORDID){
			var parent = ux.megatron.Main.getServer().getBodyBlock();
			var rec = mtx.getRecord(strRECORDID);
			this.__appBasicInfoId = rec.sysBorrowContractId;
			this.__studentId = rec.studentId;
			ux.megatron.Main.loadUIResourceAsync(parent, "ux.cdb.admin", "ux/cdb/admin/queries/loan/ui/detaildialog.xml", this,  this.dialogLoaded);
		};
		
		query.prototype.dialogLoaded = function(objEvent){
/* @JSC :: begin DEBUG */
			jsx3.log('dialogLoaded.['+this.__appBasicInfoId+','+ this.__studentId+']');
			//jsx3.log('selected record : ' + mtx.getRecordNode(strRECORDID));
/* @JSC :: end */
			objEvent.obj.loadDetails(this.__appBasicInfoId, this.__studentId);
		};
		
		query.prototype.loadFilter = function() {
        ux.megatron.Main.loadResource("ux/cdb/admin/queries/loan/" + "xml/SelFilter.xml", "ux.cdb.admin", "LoanQueryStatus", "xml");
        this.blkField_status_select.repaint();
     }
		
		query.prototype._onFilter = function(sID) {
        if(sID!="All") {
          var value = this.blkField_status_select.getText();
          this.matrix.filter("status", value);
        } else {
          this.matrix.filter();
        }
      }

    });
}